package cn.iocoder.yudao.framework.common.pojo;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 对外接口通用返回
 *
 * @param <T> 数据泛型
 */
@Data
@NoArgsConstructor
public class ApiCommonResult<T> implements Serializable {

    /**
     * 错误码
     *
     * @see ErrorCode#getCode()
     */
    private Integer code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 错误提示，用户可阅读
     *
     * @see ErrorCode#getMsg() ()
     */
    private String msg;
    private String error;
    private Long time;
    @JSONField(name = "error_time")
    private long errorTime;

    public static final Integer SUCCESS = 1;


    public static <T> ApiCommonResult<T> success(T data) {
        ApiCommonResult<T> result = new ApiCommonResult<>();
        result.code = SUCCESS;
        result.data = data;
        result.setTime(System.currentTimeMillis());
        result.setMsg("返回成功");
        return result;
    }

    public static <T> ApiCommonResult<T> error(String error) {
        ApiCommonResult<T> result = new ApiCommonResult<>();
        result.code = 0;
        result.error = error;
        result.msg = error;
        result.errorTime = System.currentTimeMillis();
        return result;
    }


}
